package com.shopCarAnli;

import java.util.Scanner;

/**
 * @ClassName ShopCarTest
 * @Description 购物车案例
 * @Author 2214361854
 * @Date 2022/2/12 22:12
 * @Version 1.0
 */
public class ShopCarTest {
    public static void main(String[] args) {
        Goods[] shopCar = new Goods[100];
        boolean gwckz = true;
        System.out.println("请您选择如下命令进行操作：");
        System.out.println("添加商品：add");
        System.out.println("查询商品：query");
        System.out.println("修改商品：update");
        System.out.println("结算商品：pay");
        System.out.println("退出系统：exit");
        Scanner sc = new Scanner(System.in);
        while(gwckz){
            System.out.print("输入命令：");
            String command = sc.next();
            switch(command){
                case "add":
                    addGoods(shopCar,sc);
                    break;
                case "query":
                    queryGoods(shopCar);
                    break;
                case "update":
                    updateGoods(shopCar,sc);
                    break;
                case "pay":
                    pay(shopCar);
                    break;
                case "exit":
                    gwckz = false;
                    break;
                default:
                    System.out.println("请重新输入命令");
            }
        }
    }

    public static void pay(Goods[] shopCar) {
        double money = 0;
        for (int i = 0; i < shopCar.length; i++) {
            Goods gs = shopCar[i];
            if(gs!=null){
                money+=(gs.getPrice()+gs.getBuyNumber());
            }
        }
        queryGoods(shopCar);
        System.out.println("购物车结算总价："+money);
    }

    public static void updateGoods(Goods[] shopCar, Scanner sc) {
        while (true){
            System.out.println("请您输入要修改商品的编号（退出101）");
            int i = sc.nextInt();
            if(i==101){
                System.out.println("退出修改购物车");
                break;
            }
            Goods gs = getGoodsById(shopCar,i);
            if(gs==null){
                System.out.println("对不起，未找到商品");
            }else{
                System.out.println("请您输入"+gs.getName()+"购买数量：");
                gs.setBuyNumber(sc.nextInt());
                System.out.println("修改完成！");
                queryGoods(shopCar);
                break;
            }
        }
    }

    public static Goods getGoodsById(Goods[] shopCar, int id){
        for (int i = 0; i < shopCar.length; i++) {
            Goods gs = shopCar[i];
            if(gs != null){
                if(gs.getId()==id){
                    return gs;
                }
            }
        }
        return null;
    }

    public static void queryGoods(Goods[] shopCar) {
        System.out.println("==========查询购物车==========");
        System.out.println("编号\t\t名称\t\t价格\t\t购买数量");
        for (int i = 0; i < shopCar.length; i++) {
            Goods gs = shopCar[i];
            if(gs != null){
                System.out.println(gs.getId()+"\t\t"+gs.getName()+"\t\t"+gs.getPrice()+"\t\t"+gs.getBuyNumber());
            }
        }
    }

    /**
     * 购物车商品添加
     * 1 手表 100 12
     * @param shopCar
     * @param sc
     */
    public static void addGoods(Goods[] shopCar, Scanner sc) {
        System.out.println("请您输入购买商品的编号（不重复） 名称 价格 数量");
        Goods gs = new Goods(sc.nextInt(), sc.next(), sc.nextDouble(), sc.nextInt());
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] == null) {
                shopCar[i]=gs;
                break;
            }
        }
        System.out.println("商品已成功添加："+gs.toString());
    }
}
